package com.asset.dispoal.file.agreementCaseData.service.commom;

import com.alibaba.excel.EasyExcel;
import com.asset.dispoal.business.bsAgreement.domain.db.mapper.AgreementMapper;
import com.asset.dispoal.file.agreementCaseData.domain.db.mapper.CaseMapper;
import com.asset.dispoal.file.agreementCaseData.domain.dto.SmsStateNo;
import com.asset.dispoal.file.agreementCaseData.dto.ImportSmsStateRequest;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.core.util.StringUtil;
import tech.mhuang.pacebox.springboot.protocol.Result;

import java.util.List;

@Component
@Slf4j
public class ImportSmsStateCommand implements BaseCommand<ImportSmsStateRequest, Result> {

    @Autowired
    AgreementMapper agreementMapper;

    @Autowired
    CaseMapper caseMapper;

    @SneakyThrows
    @Override
    public Result execute(ImportSmsStateRequest request) {
        List<SmsStateNo> smsStateNoList = EasyExcel.read(request.getFile().getInputStream()).head(SmsStateNo.class).sheet().doReadSync();
        for (SmsStateNo smsStateNo : smsStateNoList) {
            String caseId = agreementMapper.getCaseIdByContractIdCard(request.getAssetInstBatchId(), smsStateNo.getContractNo(), smsStateNo.getIdCard());
            if (StringUtil.isNotEmpty(caseId)) {
                caseMapper.updateSmsState(caseId, smsStateNo.getState());
            }
        }
        return Result.ok();
    }
}